from models.common import RWModel, OID
from typing import Optional
from enum import Enum
from config import settings


class FileType(str, Enum):
    xls = "xls"
    doc = "doc"
    ppt = "ppt"
    pdf = "pdf"

    image = "image"
    video = "video"


class FileEntity(RWModel):
    id: Optional[str]
    name: Optional[str]
    path: Optional[str]
    type: Optional[str]
    mime: Optional[str]
    display_name: Optional[str]  # 用于协助前端交互
    preview: Optional[str]  # 用于存储视频的预览图
    creator: Optional[str]

    # 只返回不存储
    url: Optional[str]

    def fill_url(self):
        if self.path:
            self.url = settings.basic['static_basic_url'] + self.path

    def unstore_url(self):
        if self.url:
            delattr(self, 'url')


class FileUploadConfig(object):
    file_type2content_type = {
        FileType.xls: ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'],
        FileType.ppt: ['application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.ms-powerpoint'],
        FileType.doc: ['application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/msword'],
        FileType.pdf: ['application/pdf'],
        FileType.image: ['image/jpeg', 'image/png'],
        FileType.video: ["video/mp4", "video/x-msvideo", "video/quicktime", "video/x-ms-wmv", "video/quicktime", "video/mpeg", "video/x-flv", "video/vnd.rn-realvideo"]
    }
    size_limit = {
        FileType.xls: 10 * 1048576,
        FileType.doc: 10 * 1048576,
        FileType.ppt: 10 * 1048576,
        FileType.pdf: 10 * 1048576,
        FileType.image: 5 * 1048576,
        FileType.video: 200 * 1048576,
    }

    content_type2file_type = {elem: k for k, v in file_type2content_type.items() for elem in v}

    @classmethod
    def get_file_type_by_content_type(cls, content_type):
        return cls.content_type2file_type.get(content_type)
